package com.lh.rabbitmq.work.lunxun;

import com.lh.rabbitmq.utils.RabbitMQUtils;
import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * 工作队列下轮询模式消费者2
 */
public class Work2 {
    public static void main(String[] args) {
        try {
            Connection connection = RabbitMQUtils.getConnection();
            Channel channel = connection.createChannel();
            // 接收消息
            channel.basicConsume("queue4", true, new DeliverCallback() {
                @Override
                public void handle(String consumerTag, Delivery message) throws IOException {
                    System.out.println("work2接收消息" + new String(message.getBody(), "UTF-8"));
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, new CancelCallback() {
                @Override
                public void handle(String consumerTag) throws IOException {
                    System.out.println("work2接收消息失败");
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
